package com.kantboot.business.bp.web.contrller;

import com.kantboot.business.bp.domain.dto.BAddDTO;
import com.kantboot.business.bp.domain.entity.BusBpGradeAndSubject;
import com.kantboot.business.bp.repository.BusBpGradeAndSubjectRepository;
import com.kantboot.business.bp.service.IBusBpGradeService;
import com.kantboot.util.common.result.RestResult;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 年级的控制器
 */
@RestController
@RequestMapping("/bus/bp/grade")
public class BusBpGradeController {

    @Resource
    private IBusBpGradeService service;

    @Resource
    private BusBpGradeAndSubjectRepository busBpGradeAndSubjectRepository;

    @RequestMapping("/getAll")
    public RestResult getAll() {
        return RestResult.success(service.getAll(),"getSuccess","获取成功");
    }

    /**
     * 绑定年级的学科
     */
    @RequestMapping("/bindSubject")
    public RestResult bindSubject(@RequestBody BAddDTO dto) {
        // 根据学科id获取
        List<BusBpGradeAndSubject> byGradeId = busBpGradeAndSubjectRepository.findByGradeId(dto.getGradeId());
        // 删除原有的学科
        busBpGradeAndSubjectRepository.deleteAll(byGradeId);
        // 保存新的学科
        busBpGradeAndSubjectRepository.saveAll(dto.getSubjectIds().stream().map(subjectId -> {
            BusBpGradeAndSubject busBpGradeAndSubject = new BusBpGradeAndSubject();
            busBpGradeAndSubject.setGradeId(dto.getGradeId());
            busBpGradeAndSubject.setSubjectId(subjectId);
            return busBpGradeAndSubject;
        }).toList());
        return RestResult.success("setSuccess","设置成功");
    }

}
